package com.qianfeng.controller;

import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Book;
import com.qianfeng.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@CrossOrigin
@ResponseBody
@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 添加书
     * @param book
     * @return
     */
    @RequestMapping(value = "/addBook")
    public Result addBook(@RequestBody Book book){

        bookService.addBook(book);
        Result result = new Result();
        return result;
    }

    /**
     * 删除书
     * @param bookId
     * @return
     */
    @RequestMapping(value = "/deleteBook")
    public Result deleteBook(Integer bookId){
        bookService.deleteBook(bookId);
        Result result = new Result();
        return result;
    }

    /**
     * 更新书
     * @param book
     * @return
     */
    @RequestMapping(value = "/updateBook")
    public Result updateBook(@RequestBody Book book){
        bookService.updateBook(book);
        Result result = new Result();
        return result;
    }

    @RequestMapping(value = "/getBookPage")
    public Result getBookPage(Integer pageNo,Integer pageSize){
       if(pageNo==null){
           pageNo=1;
       }
       if(pageSize==null){
           pageSize=5;
       }
       Page page = bookService.getBookPage(pageNo,pageSize);
       Result result = new Result();
       result.setData(page);
       return result;

    }


}
